<?php

/**
 * @package Analyser
 * @author Yves Peeters
 */

/**
 * Curl based class to spider webpages
 */
Class PHP_Curl_Crawler {

    private $default_useragent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
    private $curl;

    function close() {
        curl_close($this->curl);
    }

    function init() {
        $this->curl = curl_init();
    }

    /**
     * This is used for login.
     */
    function logIn($loginActionUrl, $parameters) {
        curl_setopt($this->curl, CURLOPT_URL, $loginActionUrl);
        curl_setopt($this->curl, CURLOPT_POST, 1);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $parameters);
        curl_setopt($this->curl, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_exec($this->curl);
    }

    /**
     * checks http headers to detect if a pages exists
     * @param String $url
     * @return boolean
     */
    function pageExists($url) {
        $status = get_headers($url);

        if (isset($status[0])) {
            if ($status[0] == "HTTP/1.1 200 OK") {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    function pageMoved($url) {
        $status = get_headers($url,1);
        if($status[0] == "HTTP/1.1 301 Moved Perminantly"){return true;}
        else{return false;}
    }

    function getRedirectLocation($url) {
        $headers = get_headers($url, 1);
        if (isset($headers['Location'])) {
            return $headers['Location'];
        } else {
            return "";
        }
    }

    /**
     * returns the http headers for a page
     * 
     * requests a page,only the headers, to see if it's available
     * @param String $url
     * @return Array
     */
    function httpHeaders($url) {
        @curl_setopt($this->curl, CURLOPT_URL, $url);
        @curl_setopt($this->curl, CURLOPT_HEADER, TRUE);
        @curl_setopt($this->curl, CURLOPT_NOBODY, TRUE);
        @curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, FALSE);
        @curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
        $status = array();
        // preg_match('/HTTP\/.* ([0-9]+) .*/', @curl_exec($this->curl) , $status);
        @curl_exec($this->curl);
        $status = @curl_getinfo($this->curl);
        return $status;
    }

    /**
     * gets the content of url
     * @param String $url
     * @return String
     */
    function getContent($url) {
        curl_setopt($this->curl, CURLOPT_USERAGENT, $this->default_useragent);
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); // @todo proper fix for verification http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/
        curl_setopt($this->curl, CURLOPT_COOKIESESSION,  TRUE);
        curl_setopt($this->curl, CURLOPT_COOKIEJAR, 'cookie.txt');        
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($this->curl, CURLOPT_HEADER, TRUE);
        $content = curl_exec($this->curl);
        return $content;
    }

    /**
     * sends post var to url
     * @param String $url
     * @param String $postvar
     */
    function postToPage($url, $postvar) {
        curl_setopt($this->curl, CURLOPT_URL, "$url");
        curl_setopt($this->curl, CURLOPT_POST, 1);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, "url=$postvar");
        curl_exec($this->curl);
    }

    /**
     * returns true if a protocol is used in url
     * <p>Exmpl.</p>
     * <code>
     * $crl->hasProtocol("http://www.google.com");
     * => returns true
     * $crl->hasProtocol("www.google.com");
     * => returns false
     * </code>
     * @param String $url
     * @return Boolean
     */
    function hasProtocol($url) {
        return strpos($url, "//");
    }

    /**
     * returns the domain from url
     * @param String $url
     * @return String
     */
    function getDomain($url) {
        return substr($url, 0, strrpos($url, "/"));
    }

    /**
     * Convert the link as It should be
     */
    function convertLink($domain, $url, $link) {

        if ($this->hasProtocol($link)) {
            return $link;
        } elseif (($link == '#') || ($link == "/")) {
            return $url;
        }
        //else if((strpos($link,'/'))==0)
        else if (substr($link, 0, 1) == "/") {
            return $domain . $link;
        } else {
            return $domain . "/" . $link;
        }
    }

    /**
     * Gets the page size
     * @param String $url
     * @return Int
     */
    function getPageSize($url) {
        curl_setopt($this->curl, CURLOPT_URL, $url);
        curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_exec($this->curl);
        return curl_getinfo($this->curl, CURLINFO_SIZE_DOWNLOAD);
    }

    function getInfo() {
        return curl_getinfo($this->curl);
    }

}

?>